<?php
class NewsController extends AppController {
    var $name = 'News';
    var $uses = array('New');
    function beforeFilter() {
        parent::beforeFilter();
    }
    function beforeRender() {
        @$this->set('meta_title', 'Categories');
        @$this->set('meta_description', 'Categories');
        @$this->set('meta_keywords', 'Categories');
    }

    public function index(){
        if( $this->RequestHandler->isRss() ){
            $posts = $this->New->find('all', array('limit' => 20, 'order' => 'New.created DESC'));
            return $this->set(compact('posts'));
        }

        // this is not an Rss request, so deliver
        // data used by website's interface
        $this->paginate['New'] = array('order' => 'New.created DESC', 'limit' => 10);
        
        $posts = $this->paginate();
        $user = $this->Auth->user();
        $can_add = $user['User']['role'] == 0;
        $this->set(compact(array('posts', 'can_add')));
    }

    public function add() {
        $user=$this->Auth->user();
        if ($user['User']['role'] == 1) {
            $this->Session->setFlash('Invalid url');
            $this->redirect('/');
        }
        if(!$user || $user['User']['role'] > 0){
            $this->redirect('/');
        }
        if (!empty($this->data)) {
            $this->New->create();
            if ($this->New->save($this->data)) {
                $url='/news/index/';
                $this->Session->setFlash(__('The story has been saved', true));
                $this->redirect($url);
            }
            else {
                $this->Session->setFlash(__('The story could not be saved. Please, try again later.', true));
            }
        }
    }
}
